

//
//  LoginMainAction.m
//  FinancialStore
//
//  Created by 王晓萌 on 16/8/8.
//  Copyright © 2016年 zhenqiu. All rights reserved.
//

#import "LoginMainAction.h"
#import "LoginQuicklyController.h"
#import "LoginForgetPwdController.h"
#import "LoginUserRegistController.h"
#import "ClientApi.h"
#import "CacheHelper.h"
#import "NotificationCenterManager.h"


static id getLoginConfig(){
    
    NSArray *array = @[@{@"title" : @"请输入手机号",
                         @"img" :UI_IMAGE_LOGIN_PHONE_ICON
                         },
                       @{@"title" : @"请输入密码",
                         @"img" :UI_IMAGE_LOGIN_PWD_ICON
                         }
                       ];
    return array;
}

static id getQuickData(){
    
    NSArray *array = @[@{@"title" : @"请输入手机号",
                         @"img" :UI_IMAGE_LOGIN_PHONE_ICON
                         },
                       @{@"title" : @"请输入验证码",
                         @"img" :UI_IMAGE_LOGIN_PWD_ICON,
                         @"cellType" : @1,
                         }
                       ];
    return array;
}
static id getForgetPwdData(){
    
    NSArray *array = @[@{@"title" : @"请输入手机号",
                         @"img" :@""
                         },
                       @{@"title" : @"请输入验证码",
                         @"img" :@"",
                         @"cellType" : @1,
                         },
                       @{@"title" : @"6位以上字母或数字密码",
                         @"img" :@""
                         },
                       @{@"title" : @"请确认密码",
                         @"img" :@""
                         }
                       ];
    return array;
}


@implementation LoginMainAction

- (instancetype)init{
    if (self = [super init]) {
        
    }
    return self;
}

-(void)start{
    
}

-(void)dealloc{
    
}

+ (NSArray *)getLocalConfig:(NSInteger)type{
    NSArray *array = nil;
    switch (type) {
        case kLocalConfigLogin:
            array = [LoginMainAction getLoginData];
            break;
        case kLocalConfigQuick:
            array = [LoginMainAction getQuickData];
            break;
        case kLocalConfigForget:
            array = [LoginMainAction getForgetPwdData];
            break;
        default:
            break;
    }
    return array;
}

+ (NSArray *)getLoginData{
    NSArray *array = getLoginConfig();
    return array;
}

+ (NSArray *)getQuickData{
    NSArray *array = getQuickData();
    return array;
}

+ (NSArray *)getForgetPwdData{
    NSArray *array = getForgetPwdData();
    return array;
}
+ (void)jumpWith:(UIViewController *)viewController index:(NSInteger)index andIsRn:(BOOL)isRn{
  LoginBaseController *login = (LoginBaseController *)viewController;
  if (index == 0) {
    LoginQuicklyController *lqVc = [[LoginQuicklyController alloc] init];
    //        [viewController presentViewController:lqVc animated:YES completion:^{
    //        }];
    lqVc.goBack = login.goBack;
    if (isRn) {
      lqVc.isRn = YES;
    }
    [viewController.navigationController pushViewController:lqVc animated:YES];
  }else if (index == 1){
    LoginForgetPwdController *lfpVc = [[LoginForgetPwdController alloc] init];
    //        [viewController presentViewController:lfpVc animated:YES completion:^{
    //        }];
    lfpVc.goBack = login.goBack;
    if (isRn) {
      lfpVc.isRn = YES;
    }
    [viewController.navigationController pushViewController:lfpVc animated:YES];
  }else if (index == 2){
    LoginUserRegistController *lurVc = [[LoginUserRegistController alloc] init];
    //        [viewController presentViewController:lurVc animated:YES completion:^{
    //        }];
    lurVc.goBack = login.goBack;
    if (isRn) {
      lurVc.isRn = YES;
    }    [viewController.navigationController pushViewController:lurVc animated:YES];
  }

}


+ (void)jumpWith:(UIViewController *)viewController index:(NSInteger)index{
    LoginBaseController *login = (LoginBaseController *)viewController;
    if (index == 0) {
        LoginQuicklyController *lqVc = [[LoginQuicklyController alloc] init];
//        [viewController presentViewController:lqVc animated:YES completion:^{
//        }];
        lqVc.goBack = login.goBack;
        [viewController.navigationController pushViewController:lqVc animated:YES];
    }else if (index == 1){
        LoginForgetPwdController *lfpVc = [[LoginForgetPwdController alloc] init];
//        [viewController presentViewController:lfpVc animated:YES completion:^{
//        }];
        lfpVc.goBack = login.goBack;
        [viewController.navigationController pushViewController:lfpVc animated:YES];
    }else if (index == 2){
        LoginUserRegistController *lurVc = [[LoginUserRegistController alloc] init];
//        [viewController presentViewController:lurVc animated:YES completion:^{
//        }];
        lurVc.goBack = login.goBack;
        [viewController.navigationController pushViewController:lurVc animated:YES];
    }
}


#pragma mark network request 

-(void)getVerifyCode:(NSString*)phone withCallBack:(ActionCallBack)callBack{
    
    
   
    [[ClientApi getInstance] getVerifyCode:phone withTelid:1 withUid:0 withIsVerify:0 withCallBack:^(id  _Nullable responseObject, NSError * _Nullable error) {
        
        if(error){
            
            GYNLog(@"getVerify Error = %@",[error description]);
            GYNLog(@"getVerify Error = %@",[responseObject description]);
            NSString * error_msg = [self getErrorMsg:error];
            callBack(error_msg);
            
        }else{
            callBack(nil);
        }
    }];
    
}

-(void)quickLogin:(NSString*)phone withCode:(NSString*)verifyCode withCallBlock:(ActionCallBack)callBack{
    [[ClientApi getInstance] login:phone withVerify:1 withChecksms:verifyCode withPasswd:nil withCallBack:^(id  _Nullable responseObject, NSError * _Nullable error) {
        
        if(error){
            
            GYNLog(@"login Error = %@",[error description]);
            NSString * error_msg = [self getErrorMsg:error];
            callBack(error_msg);
        }else{
            
            GYNLog(@"login Success = %@",[responseObject description]);
            
            id uid = [responseObject valueForKey:@"uid"];
            id avatar = [responseObject valueForKey:@"avatar"];
            [[CacheHelper getInstance] cacheUserId:uid];
            [[CacheHelper getInstance] cachePhoneNumber:phone];
            [[CacheHelper getInstance] cacheUserAvatar:avatar];
            [NotificationCenterManager sendNotification:USER_INFO_NOTIFICATION];
            callBack(nil);
        }
        
    }];
}
//快速登录
-(void)loginWithUserName:(NSString *)phone withPassWord:(NSString *)passWord withCallBlock:(ActionCallBack)callBack{
    [[ClientApi getInstance]login:phone withVerify:0 withChecksms:nil withPasswd:passWord withCallBack:^(id  _Nullable responseObject, NSError * _Nullable error) {
        
        if(error){
            
            GYNLog(@"login Error = %@",[error description]);
            NSString * error_msg = [self getErrorMsg:error];
            callBack(error_msg);
        }else{
            
            GYNLog(@"login Success = %@",[responseObject description]);
            
            id uid = [responseObject valueForKey:@"uid"];
            id avatar = [responseObject valueForKey:@"avatar"];
            [[CacheHelper getInstance] cacheUserId:uid];
            [[CacheHelper getInstance] cachePhoneNumber:phone];
            [[CacheHelper getInstance] cacheUserAvatar:avatar];
            [NotificationCenterManager sendNotification:USER_INFO_NOTIFICATION];
            callBack(nil);
        }
        
    }];
}
//注册验证码
-(void)registerForProvingWithPhoneNumber:(NSString *)phoneNumber withCallBack:(ActionCallBack)callBack{
    [[ClientApi getInstance]getMeVerifyPhoneNumber:phoneNumber withTeild:1 withCallBack:^(id  _Nullable responseObject, NSError * _Nullable error) {
        if (error) {
            NSString * error_msg = [self getErrorMsg:error];
            callBack(error_msg);
        }else {
            [[CacheHelper getInstance] cachePhoneNumber:phoneNumber];
            callBack(nil);
        }
        
    }];
}
-(void)registerForPhoneNumber:(NSString *)phoneNumber withChecksms:(NSString *)checksms withPasswd:(NSString *)passwd withRegisterPasswd:(NSString *)registerPasswd withCallBack:(ActionCallBack)callBack{
   
    [[ClientApi getInstance]loginWithRegister:phoneNumber withChecksms:checksms withPasswd:passwd withConfirmPasswd:registerPasswd withCallBack:^(id  _Nullable responseObject, NSError * _Nullable error) {
        if (error) {
            NSString * error_msg = [self getErrorMsg:error];
            callBack(error_msg);
        }else {
            id uid = [responseObject valueForKey:@"uid"];
            id avatar = [responseObject valueForKey:@"avatar"];
            [[CacheHelper getInstance] cacheUserId:uid];
            [[CacheHelper getInstance] cacheUserAvatar:avatar];
            [[CacheHelper getInstance] cachePhoneNumber:phoneNumber];
            [NotificationCenterManager sendNotification:USER_INFO_NOTIFICATION];
            callBack(nil);
        }
        
    }];
}
- (void)forgotWithPhoneNumber:(NSString *)phoneNumber withCallBack:(ActionCallBack)callBack{
    [[ClientApi getInstance]getMeVerifyPhoneNumber:phoneNumber withTeild:5 withCallBack:^(id  _Nullable responseObject, NSError * _Nullable error) {
        if (error) {
            NSString * error_msg = [self getErrorMsg:error];
            callBack(error_msg);
        }else {
            [[CacheHelper getInstance] cachePhoneNumber:phoneNumber];
            [NotificationCenterManager sendNotification:USER_INFO_NOTIFICATION];
            callBack(nil);
        }
     
    }];
}
- (void)forGotForLoginWithPhoneNumber:(NSString *)phoneNumber withChecksms:(NSString *)checksms withPasswd:(NSString *)passwd withConfirmPasswd:(NSString *)confirmPasswd withCallBack:(ActionCallBack)callBack{
    [[ClientApi getInstance]forGotToLoginWithPhoneNumber:phoneNumber withChecksms:checksms withPasswd:passwd withConfirmPasswd:confirmPasswd withCallBack:^(id  _Nullable responseObject, NSError * _Nullable error) {
        if (error) {
            NSString * error_msg = [self getErrorMsg:error];
            callBack(error_msg);
        }else {
            
            [[CacheHelper getInstance] cachePhoneNumber:phoneNumber];
            [NotificationCenterManager sendNotification:USER_INFO_NOTIFICATION];
            callBack(nil);
        }
        
    }];
}
- (void)gorGoBackToMeViewController {
    AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
    UITabBarController *tab = (UITabBarController *)appDelegate.window.rootViewController;
    tab.selectedIndex = 3;
}

//不用验证快速登录
+ (void)quickLoginWithUserName:(NSString *)phone withCallBlock:(ActionCallBack)callBack{
  [[ClientApi getInstance]login:phone withVerify:0 withChecksms:nil withPasswd:nil andIsJump:1 withCallBack:^(id  _Nullable responseObject, NSError * _Nullable error) {
    if(error){
      GYNLog(@"login Error = %@",[error description]);
      NSString * error_msg = [[error userInfo] valueForKey:NSLocalizedDescriptionKey];
      callBack(error_msg);
    }else{
      
      GYNLog(@"login Success = %@",[responseObject description]);
      
      id uid = [responseObject valueForKey:@"uid"];
      id avatar = [responseObject valueForKey:@"avatar"];
      [[CacheHelper getInstance] cacheUserId:uid];
      [[CacheHelper getInstance] cachePhoneNumber:phone];
      [[CacheHelper getInstance] cacheUserAvatar:avatar];
      callBack(nil);
    }
    
  }];
}
@end
